package com.atguigu.rabbitmq.producer.service;

import com.alibaba.fastjson.JSON;
import com.atguigu.rabbitmq.producer.config.RabbitDLXConfiguration;
import com.atguigu.rabbitmq.producer.entiry.GlobalCorrelationData;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * Author ChenYongSheng
 * Date 2025/11/2 15:45
 */
@Service
public class RabbitMQService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void sendMsg(String exchange, String routingKey, String content, GlobalCorrelationData globalCorrelationData) {
        String UUIDString = UUID.randomUUID().toString().replace("-", "");

//        GlobalCorrelationData globalCorrelationData=new GlobalCorrelationData();

        if (globalCorrelationData.getReCount() == 0) {
            globalCorrelationData.setId(UUIDString);
            globalCorrelationData.setExchange(exchange);
            globalCorrelationData.setRoutingKey(routingKey);
            globalCorrelationData.setContent(content);
        }

        // 往缓存中也存一份，如有有异常直接拿 id去缓存找对应的数据
        redisTemplate.opsForValue().set(globalCorrelationData.getId(), JSON.toJSONString(globalCorrelationData), 1, TimeUnit.HOURS);

        rabbitTemplate.convertAndSend(globalCorrelationData.getExchange(), globalCorrelationData.getRoutingKey(), globalCorrelationData.getContent(), globalCorrelationData);
    }

    /**
     * 设置延迟队列方法
     *
     * @param content    存放内容
     * @param delayTime  延迟时间
     */
    public void sendDelayMsg(String content, Integer delayTime) {
        String UUIDString = UUID.randomUUID().toString().replace("-", "");

        GlobalCorrelationData globalCorrelationData = new GlobalCorrelationData();

        globalCorrelationData.setId(UUIDString);
        globalCorrelationData.setExchange(RabbitDLXConfiguration.exchange_delay_order);
        globalCorrelationData.setRoutingKey( RabbitDLXConfiguration.routing_delay_order);
        globalCorrelationData.setContent(content);
        // 如果是死信队列加上判断 ，就不会走失败重试机制
        globalCorrelationData.setIsDelay(true);

        // 往缓存中也存一份，如有有异常直接拿 id去缓存找对应的数据
        redisTemplate.opsForValue().set(globalCorrelationData.getId(), JSON.toJSONString(globalCorrelationData), 1, TimeUnit.HOURS);

        rabbitTemplate.convertAndSend(globalCorrelationData.getExchange(), globalCorrelationData.getRoutingKey(), globalCorrelationData.getContent(), new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setDelay(delayTime);
                return message;
            }
        }, globalCorrelationData);
    }
}
